﻿import time

import Hobot.GPIO as GPIO
import sys
import json


# 读取定义的gpio接口
track_light_left = int(sys.argv[1])
track_light_min_left = int(sys.argv[2])
track_light_mid = int(sys.argv[3])
track_light_min_right = int(sys.argv[4])
track_light_right = int(sys.argv[5])


def setup():
    GPIO.cleanup()
    GPIO.setmode(GPIO.BOARD)

    #输出读取数据
    print("左侧{0} 微左侧{1} 中间{2} 微右侧{3} 右侧{4}".format(track_light_left,track_light_min_left,track_light_mid,track_light_min_right,track_light_right))

    GPIO.setup(track_light_left, GPIO.IN,initial=GPIO.LOW)
    GPIO.setup(track_light_min_left, GPIO.IN,initial=GPIO.LOW)
    GPIO.setup(track_light_mid, GPIO.IN,initial=GPIO.LOW)
    GPIO.setup(track_light_min_right, GPIO.IN,initial=GPIO.LOW)
    GPIO.setup(track_light_right, GPIO.IN,initial=GPIO.LOW)
   

    print("设置接口完毕")

def loop():


    # 记忆变量
    rem_state = None
    
    # 状态容器-声明
    conditions = {(False,False,False,False,False):"None"}
    #状况2-中路反馈
    conditions.update({(False,False,True,False,False):"直行"})
    #状况3-右转反馈
    conditions.update({(True,False,False,False,False):"右转"})
    # 状况4-左转反馈
    conditions.update({(False,False,False,False,True):"左转"})
    # 状况5-微右转反馈
    conditions.update({(False,True,False,False,False):"右微调"})
    # 状况6-微左转反馈
    conditions.update({(False,False,False,True,False):"左微调"})

    # 额外状态补充1-左和微左同时有信号
    conditions.update({(False,True,True,False,False):"左转"})
    # 额外状态补充2-右和微右同时有信号
    conditions.update({(False,False,False,True,True):"右转"})
    # 额外状态补充3-中和微左同时有信号
    conditions.update({(False,True,True,False,False):"左微调"})
    # 额外状态补充4-中和微右同时有信号
    conditions.update({(False,False,True,True,False):"右微调"})


    # 有效状态标记
    valid_state = False
    
    while True:
        light_left = GPIO.input(track_light_left)==GPIO.LOW
        light_min_left = GPIO.input(track_light_min_left)==GPIO.LOW
        light_mid = GPIO.input(track_light_mid)==GPIO.LOW
        light_min_right = GPIO.input(track_light_min_right)==GPIO.LOW
        light_right = GPIO.input(track_light_right)==GPIO.LOW

        print("接口输入状况  {0}:{1} {2}:{3} {4}:{5} {6}:{7} {8}:{9}".format(track_light_left,light_left,track_light_min_left,light_min_left,track_light_mid,light_mid,track_light_min_right,light_min_right,track_light_right,light_right))

        # print("左侧{0} 左侧最小{1} 中间{2} 右侧最小{3} 右侧{4}".format(light_left,light_min_left,light_mid,light_min_right,light_right))
        # 字典状态映射结果
        # for condition,state in conditions.items():
        #     if condition == (light_left,light_min_left,light_mid,light_min_right,light_right):
        #         # print("当前状态：{0}".format(state))
        #         print("接口输入状况  {0}:{1} {2}:{3} {4}:{5} {6}:{7} {8}:{9}".format(track_light_left,light_left,track_light_min_left,light_min_left,track_light_mid,light_mid,track_light_min_right,light_min_right,track_light_right,light_right))
        #         rem_state = condition #保留保底状态
        #         valid_state = True
        #         break
        
        # if  not valid_state:
            # print("当前状态：{0}".format(rem_state))
            
        # 执行实际逻辑 TODO
        
        # 设置间隔
        time.sleep(0.2)
def destroy():

    GPIO.cleanup()



if __name__ == '__main__':
    
    try:
        setup()
        loop()
    except KeyboardInterrupt:
        destroy()
    finally:
        destroy()